package com.huluobo.h3kotlinproject.home

import androidx.lifecycle.MutableLiveData
import com.huluobo.h3kotlinproject.base.BaseRepo
import com.huluobo.h3kotlinproject.data.GoodsListResult
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 *  Created by LC on 2023/10/21.
 */
class HomeRepo : BaseRepo() {
    fun getHomeList(
        categoryId: Int,
        currentPage: Int,
        pageSize: Int,
        success: MutableLiveData<GoodsListResult>,
        failed: MutableLiveData<String>
    ) {
        getService().getHomeGoodsList(categoryId, currentPage, pageSize).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread()).subscribe(object : Observer<GoodsListResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: GoodsListResult) {
                    success.value = t
                }
            })
    }

    fun getBanners(success: MutableLiveData<HomeBannerBeanResult>, failed: MutableLiveData<String>) {
        getService().getBanners().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<HomeBannerBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: HomeBannerBeanResult) {
                    success.value = t
                }
            })
    }

    fun getNav(success: MutableLiveData<NavGoodsBeanResult>, failed: MutableLiveData<String>) {
        getService().getNav().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<NavGoodsBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: NavGoodsBeanResult) {
                    success.value = t
                }

            })
    }
}